package com.xintoucloud.gsv.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.One;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.xintoucloud.gsv.entity.User;
import com.xintoucloud.gsv.model.UserVo;

public interface UserMapper {
	//登录
	@Select("select * from t_user where uname=#{uname} and upass=#{upass}")
	@Results({
		@Result(id= true ,column="uid",property="uid"),
		@Result(property="role",column="role_id",one=@One(select="com.xintoucloud.gsv.mapper.RoleMapper.getRole")),
		@Result(property="dept",column="dept_id",one=@One(select="com.xintoucloud.gsv.mapper.DeptMapper.getDept"))
	})
	User login(@Param("uname")String uname,@Param("upass")String upass);
	
	//查询所有用户
	@Select("select u.uid,u.uname,u.upass,u.realname,u.sex,u.card,u.birthday,u.mobile,u.dept_id,u.role_id,u.state_id,d.dname, r.rname,s.state \r\n" + 
			"from t_user u join t_dept d on(u.dept_id=d.did) join t_role r on(u.role_id=r.rid) \r\n" + 
			"join t_user_state s on(u.state_id=s.sid) order by u.uid asc;")
	List<UserVo> userVos();

	//查询所有用户并分页
	@Select("select u.uid,u.uname,u.upass,u.realname,u.sex,u.card,u.birthday,u.mobile,u.dept_id,u.role_id,u.state_id,d.dname, r.rname,s.state \r\n" + 
			"from t_user u join t_dept d on(u.dept_id=d.did) join t_role r on(u.role_id=r.rid) \r\n" + 
			"join t_user_state s on(u.state_id=s.sid) order by u.uid asc limit #{pageIndex},#{size};")
	List<UserVo> userVosByPageAndSize(@Param("pageIndex")int pageIndex,@Param("size")int size);
	
	//添加用户
	@Insert("insert into t_user(uid,uname,upass,realname,sex,birthday,card,mobile,dept_id,role_id,state_id) values(null,#{uname},#{upass},#{realname},#{sex},#{birthday},#{card},#{mobile},#{dept_id},#{role_id},#{state_id});")
    @Options(keyProperty = "uid",useGeneratedKeys = true)
	void save(UserVo userVo);
	
	@Delete("delete from t_user where uid=#{uid}")
	int userDelete(int uid);
	
	@Update("update t_user set uname=#{uname},realname=#{realname},sex=#{sex},card=#{card},birthday=#{birthday},mobile=#{mobile},dept_id=#{dept_id},role_id=#{role_id},state_id=#{state_id} where uid=#{uid}")
	int userUpdate(UserVo userVo);
	
	@Select("select count(*) from t_user")
	long count();
}
